
<template>
    <div>      
        <Row>
            <Col span="17"><h1>平台管理账号</h1></Col>             
        </Row>  
        <Table :columns="columns1" :data="data1"></Table>
        <div style="margin: 10px;overflow: hidden">
            <div style="float: right;">
                <Page :total="dataCount1" :page-size="pageSize" show-total @on-change="changePage1"></Page>
            </div>
        </div>
          <router-link to="addService">
          </router-link>
        <div><h3>初始连接（联合创始人）: </h3>http://m.mirrorcare.cn/dist/index.html#/registerFirst</div>
        <div><h3>初始连接（执行董事）: </h3>http://m.mirrorcare.cn/dist/index.html#/registerSecond</div>
        <div><h3>初始连接（合伙人）: </h3>http://m.mirrorcare.cn/dist/index.html#/registerThird</div>
         <h1>添加用户</h1>
               <div class="con">
                <div class="text">
                    <span>用户名</span>
                    <Input v-model="username" style="width:260px"></Input>
                </div>
                <div class="text">
                    <span>密码</span>
                    <Input v-model="password" style="width:260px"></Input>
                </div>
                 <div class="text">
                    <span>全名</span>
                    <Input v-model="name" style="width:260px"></Input>
                </div>
                  <div class="text">
                    <span>用户组</span>
                    <Select
                        v-model="select1"
                        filterable
                        style="width:260px">
                        <Option v-for="option in server" :value="option.value" :key="option.value">{{option.label}}</Option> 
                    </Select>
                  </div>
                <Button type="primary" @click="ok">保存</Button>                
               </div>         
    </div>
</template>
<script>
export default {
    data() {
        return {
            username:'',
            password:'',
            name:'',
            select1:'',
            dataCount1:0,
            pageSize:6,
            historyDate1:[],
            server:[],
            columns1: [
                {
                    title: "管理员姓名",
                    key: "name"
                },
                {
                    title: "用户名",
                    key: "username"
                },
                 {
                    title: "密码",
                    key: "password"
                },
                {
                    title: "用户组",
                    key: "mname"
                },
                 {
                    title: "权限",
                    key: "mid"
                },
                {
                    title: "编辑",
                    key: "action",
                    width: 150,
                    align: "center",
                    render: (h, params) => {
                        if (params.index === 0) {
                            return;
                        }
                        return h("div", [
                            h(
                                "Button",
                                {
                                    props: {
                                        type: "error",
                                        size: "small"
                                    },
                                    on: {
                                        click: () => {
                                            this.remove(params.index);
                                        }
                                    }
                                },
                                "删除"
                            ),                            
                        ]);
                    }
                }
            ],
            data1: [
                {
                    name: "John Brown",
                    age: 18,
                    address: "New York No. 1 Lake Park",
                    date: "2016-10-03"
                },
                {
                    name: "Jim Green",
                    age: 24,
                    address: "London No. 1 Lake Park",
                    date: "2016-10-01"
                },
                {
                    name: "Joe Black",
                    age: 30,
                    address: "Sydney No. 1 Lake Park",
                    date: "2016-10-02"
                },
                {
                    name: "Jon Snow",
                    age: 26,
                    address: "Ottawa No. 2 Lake Park",
                    date: "2016-10-04"
                }
            ]
        };
    },
    mounted() {
        let mid = localStorage.getItem("mid");
        if (mid == 'undefined' || mid == ''){
            alert("您还没有登陆");
            this.$router.push("admin");
            return;
        }
        if (mid.indexOf('5') < 0){
            alert("您没有权限");
            return;
        }
        this.getManagement();
        this.addOption();
    },
    methods: {
        changePage1(index){
            let start = (index - 1) * this.pageSize;
            let end = index * this.pageSize;
            this.data1 = this.historyDate1.slice(start, end);
        },
        addOption(){
            let qs = require('qs');
            this.axios({
                method: "post",
                url: `${this.adminUrl}/getPowerManagement`,
                data: qs.stringify({})
                }).then(response => {
                    if ((response.data.result == 200)) {
                        let length = response.data.data.length;
                        let harray = [];
                        for (let i = 0; i < length; i++){
                            harray.push(
                                {value:response.data.data[i].id, label:response.data.data[i].name}
                            );
                        }
                        this.server = harray;
                    }else{
                        alert("请求失败");
                    }
                }).catch(err =>{
                    console.log(err);
            });
        },
        getManagement(){
            let qs = require('qs');
            this.axios({
                method: "post",
                url: `${this.adminUrl}/getAdmins`,
                data: qs.stringify({})
                }).then(response => {
                    if ((response.data.result == 200)) {
                        this.historyDate1 = response.data.data;
                        this.dataCount1 = this.historyDate1.length;
                        if (this.historyDate1.length <= this.pageSize){
                            this.data1 = this.historyDate1;
                        }else{
                            this.data1 = this.historyDate1.slice(0, this.pageSize);
                        }
                    }else{
                        alert("请求失败");
                    }
                }).catch(err =>{
                    console.log(err);
            });
        },
        remove(index){
            let qs = require('qs');
            this.axios({
                method: "post",
                url: `${this.adminUrl}/deleteAdmins`,
                data: qs.stringify({id:`${this.data1[index].id}`, aid:localStorage.getItem("aid")})
                }).then(response => {
                    if ((response.data.result == 200)) {
                        this.data1.splice(index, 1); 
                    }else{
                        alert("请求失败");
                    }
                }).catch(err =>{
                    console.log(err);
            });
        },
        ok(){
            let qs = require('qs');
            this.axios({
                method: "post",
                url: `${this.adminUrl}/insertAdmins`,
                data: qs.stringify({username:this.username, password:this.password, name:this.name, mid:this.select1, aid:localStorage.getItem("aid")})
                }).then(response => {
                    if ((response.data.result == 200)) {
                        this.getManagement();
                        alert("添加用户成功");
                    }else{
                        alert("请求失败");
                    }
                }).catch(err =>{
                    console.log(err);
            });
        }
    }
};
</script>

   



<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
h1 {
    margin-bottom: 20px;
    margin-top:20px;
}
button {
    margin-top: 50px;
}
span{
    display: inline-block;
    width: 100px;
    font-size: 16px;
}
.text{
    margin-bottom: 10px;
}

</style>
